package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.ExperienceService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Created by xiaoji on 2016/5/3.
 * 经验
 */
@Controller
@RequestMapping(value = "/experienceControl")
public class ExperienceControl {
    private static Logger log = LoggerFactory.getLogger(ExperienceControl.class);
    @Autowired
    private ExperienceService experienceService;

    @Autowired
    private ConsumerService consumerService;

    /**
     * 我的经验
     * @param modelMap
     * @param param
     * @param s 开始序列
     * @param n 获取条数
     */
    @RequestMapping(value = "/experienceList",method = RequestMethod.GET)
    public void experienceList(ModelMap modelMap,@RequestParam Map<String,String> param,Integer s,Integer n) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("experienceList",experienceService.experienceList(param.get("name"),s,n,String.valueOf(map.get("id"))));
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 删除我的经验
     * @param modelMap
     * @param param 参数
     * @param experience_id 经验id
     */
    @RequestMapping(value = "/delExp",method = RequestMethod.GET)
    public void delExp(ModelMap modelMap,@RequestParam Map<String,String> param,Long experience_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            experienceService.delExp(experience_id, String.valueOf(map.get("id")));
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 发布经验
     * @param modelMap
     * @param param 参数
     * @param experience_id 经验Id
     */
    @RequestMapping(value = "/publishExp",method = RequestMethod.GET)
    public void publishExp(ModelMap modelMap,@RequestParam Map<String,String> param,Long experience_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            experienceService.publishExp(experience_id, String.valueOf(map.get("id")));
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
    /**
     * 获取经验内容
     * @param modelMap
     * @param param
     * @param experience_id 经验id
     */
    @RequestMapping(value = "/getExperienceContent",method = RequestMethod.GET)
    public void getExperienceContent(ModelMap modelMap,@RequestParam Map<String,String> param,Long experience_id) {
        try {
            modelMap.addAttribute("articleContent",experienceService.getExperienceContent(experience_id));
        }catch (Exception e){
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
